STEP 11: Let's create a random index to pick a middle!
In LOGIC, go to and drag Random Integer to the bottom of your program.
Change the variable name from my_var to random_body .
Change the range in .randint() to 0, 5 .
Remember: there are 6 items in the list, so the first index is 0 and the last index is 5.
To navigate the page using the TAB key, first press ESC to exit the code editor.
########################################################################
# PLACE LIST CREATIONS HERE #
########################################################################
stage_list = ["moon", "mars", "jupiter", "pluto", "space", "city"]
head_list = ["head1", "head2", "head3", "head4", "head5", "head6"]
body_list = ["middle1", "middle2", "middle3", "middle4", "middle5", "middle6"]
########################################################################
# PLACE ALL OTHER CODE BELOW THIS #
########################################################################
stage_image = stage_list[1]
stage.set_background(stage_image)
random_head = random.randint(0, 5)
head_image = head_list[random_head]
# sprite = codesters.Sprite("image", x, y)
head = codesters.Sprite(head_image, 0, 83)
t = codesters.Teacher()
try:
ranges = t.get_parameters_for_function('randint')
tval1 = int(ranges[1][0])
tval2 = int(ranges[1][1])
except:
tval1 = "DNE"
tval2 = "DNE"
try:
tval3 = random_body
except:
tval3 = "DNE"
t1 = TestObjective()
t1.add_success(tval1 == 0 and tval2 == 5 and tval3 != "DNE", "Great job!")
t1.add_failure(tval1 == "DNE", "Did you add a Random Integer?")
t1.add_failure(tval1 == 1 or tval2 == 10, "Did you change the range in .randint() to 0,5?")
t1.add_failure(tval3 == "DNE", "Did you rename the random variable random_body?")
tester = TestManager()
tester.add_test_list([t1])
tester.run_tests()
tester.display_first_feedback()
Run Code
Activity Submitted!
Submit Work
Next Activity
Stop Running Code
Show Chart
Show Console
Reset Code Editor
Codesters How To (opens in a new tab)